﻿#include "widget.h"
#include "ui_widget.h"
#include <QGraphicsRectItem>
#include <GraphicsBoxItem.h>
#include <GraphicsBoxBuilder.h>
#include <GraphicsImageBuilder.h>
#include <GraphicsDrawingBuilder.h>
#include <QScrollBar>
#include <QOpenGLWidget>
#include <QTimer>
#include <QDebug>


Widget::Widget(QWidget* parent)
	: QWidget(parent)
	, ui(new Ui::Widget)
{
	ui->setupUi(this);
	showMaximized();
	ui->label_8->hide();
	m_scene = new GraphicsScene(ui->graphicsView, this);
	connect(m_scene, &GraphicsScene::sigSceneMousePos, this, &Widget::onSceneMousePos);
	connect(m_scene, &GraphicsScene::sigBoxMousePos, this, &Widget::onBoxMousePos);
	connect(m_scene, &GraphicsScene::sigImageInfo, this, &Widget::onImageInfo);
	connect(m_scene, &GraphicsScene::sigBoxInfo, this, &Widget::onBoxInfo);

	// windows开启opengl渲染
#ifdef Q_OS_WIN
	QSurfaceFormat surfaceFormat;
	surfaceFormat.setSamples(5);//多重采样
	QOpenGLWidget* glWget = new QOpenGLWidget();
	glWget->setFormat(surfaceFormat);
	ui->graphicsView->setViewport(glWget);
#endif // Q_OS_WIN

	// 将按钮放在opengl窗口上，关键代码，btn->show()
	//QPushButton* btn = new QPushButton("================", glWget);
	//btn->setGeometry(QRect(QPoint(100, 100), QSize(100, 80)));
	//glWget->setAttribute(Qt::WA_TransparentForMouseEvents, false); // 确保 OpenGL 控件可以接收鼠标事件
	//btn->show();

	ui->graphicsView->init();
	connect(ui->graphicsView, &GraphicsView::sigViewScaleChanged, [=] {
		qDebug() << ui->graphicsView->getItemScale();
		});
	connect(ui->graphicsView, &GraphicsView::sigOutOfView, [=](bool visable) {
		qDebug() << "minimap:" << visable;
		});
}

Widget::~Widget()
{
	delete ui;
}

void Widget::init()
{
	int choose = 0;
	if (choose == 0) {
		GraphicsBoxBuilder boxBuilder(m_scene);
		m_box = boxBuilder.build();
		m_box->setImage(QImage(":/images/Fotor_AIAvatar_2024-07-15-17-41-59.png"));
		m_box->fitToScreen();
	}
	else if (choose == 1) {
		GraphicsImageBuilder builder(nullptr, m_scene);
		m_image = builder.build();
		m_image->setImage(QImage(":/images/Fotor_AIAvatar_2024-07-15-17-41-59.png"));
	}
	else if (choose == 2) {
		GraphicsDrawingBuilder builder(nullptr, m_scene);
		m_drawingItem = builder.build();
		m_drawingItem->setImage(QImage(":/images/Fotor_AIAvatar_2024-07-15-17-41-59.png"));
		m_drawingItem->setMaskImage(QImage(":/images/bc.png"));
		m_drawingItem->fitToScreen();
	}
	else {

	}
}

void Widget::on_pushButton_2_clicked()
{
	//double scale = ui->lineEdit->text().toDouble();
	//m_box->test2(scale);
	m_scene->undo();
	m_scene->printInfo();
	//m_box->test2(1);
	//m_box->setImage(QImage("D:/Desktop/test/card_ai_avatar.jpg"));
}

void Widget::on_pushButton_3_clicked()
{
	m_box->test3();
}

void Widget::on_pushButton_4_clicked()
{
	m_box->test4();
}

void Widget::on_pushButton_5_clicked()
{
	m_drawingItem->setDrawingType(GraphicsDrawingType::Eraser);
	//m_box->test5();
	//m_box->setMode(QSize(20, 20), GraphicsBoxItem::FREE);
	//m_scene->printInfo();
}

void Widget::on_pushButton_6_clicked()
{
	m_box->test1();
}

void Widget::on_pushButton_7_pressed()
{
	ui->graphicsView->setOriginImageVisable(true);
}

void Widget::on_pushButton_7_released()
{
	ui->graphicsView->setOriginImageVisable(false);
}

void Widget::on_pushButton_7_clicked()
{
	//m_drawingItem->setMaskVisable(false);
	//ui->graphicsView->setOriginImageVisable(true);
}

void Widget::on_pushButton_8_clicked()
{
	qreal scale = ui->lineEdit->text().toDouble();
	ui->graphicsView->setItemScale(scale);
}

void Widget::on_pushButton_9_clicked()
{
	bool visabled = ui->label_8->isVisible();
	ui->label_8->setVisible(!visabled);
	// 使其进入事件循环，在ui->label_8显示或者隐藏的时候，在执行
	QTimer::singleShot(0, [=] {
		ui->graphicsView->autoFitSize();
		});

	// 切换图片
	//m_drawingItem->setImage(QImage("D:/Desktop/test/zz.jpg"));
	//const QSize viewSize = ui->graphicsView->size() * 0.9;
	//m_drawingItem->setSize(viewSize);
	//m_drawingItem->setMaskImage(QImage("D:/Desktop/test/cutout.png"));

	// 切换图片
	//m_box->setImage(QImage("D:/Desktop/test/zz.jpg"));
	//m_box->onMaskChanged(QImage("D:/Desktop/test/cutout.png"));
}

void Widget::onSceneMousePos(const QPointF& scenePos)
{
	ui->posLab->setText(QString("(%1,%2)").arg(scenePos.toPoint().x()).arg(scenePos.toPoint().y()));
}

void Widget::onBoxMousePos(const QPointF& boxPos)
{
	ui->label_7->setText(QString("(%1,%2)").arg(boxPos.toPoint().x()).arg(boxPos.toPoint().y()));
}


void Widget::onImageInfo(const QString& imageInfo)
{
	ui->label_5->setText(imageInfo);
}

void Widget::onBoxInfo(const QString& boxInfo)
{
	ui->label_3->setText(boxInfo);
}


void Widget::on_pushButton_clicked()
{
	//m_box->test1();
	m_scene->redo();
	m_scene->printInfo();
}
